import { defineStore } from "pinia";
import { computed, ref } from "vue";
import { useUserStore } from "./user";
import { insertCartAPI, getCartAPI, deleteCartAPI } from "@/apis/cart";
export const useCartStore = defineStore(
  "cart",
  () => {
    const cartList = ref([] as any);
    const userStore = useUserStore();
    const isLogin = computed(() => userStore.userInfo.token);
    const updateNewList = async () => {
      const res = await getCartAPI();
      cartList.value = res.data.result;
    };
    //添加购物车
    const addCart = async (goods: any) => {
      console.log(isLogin, "34224");
      console.log(isLogin, "34224");
      if (isLogin.value) {
        const { skuId, count } = goods;
        console.log(skuId, count, goods, "stuId, count");
        await insertCartAPI({ skuId, count });
        updateNewList();
      } else {
        // TODO: 添加商品到购物车
        const item = cartList.value.find((item: any) => {
          return item.skuId === goods.skuId;
        });
        if (item as any) {
          item.count += 1;
        } else {
          cartList.value.push(goods);
        }
      }
    };
    //删除购物车
    const deleteCart = async (skuId: any) => {
      if (isLogin) {
        await deleteCartAPI([skuId]);
        updateNewList();
      } else {
        const idx = cartList.value.findIndex((item: any) => {
          return item.skuId === skuId;
        });
        cartList.value.splice(idx, 1);
      }
    };
    //计算总数量
    const allCount = computed(() =>
      cartList.value.reduce((total: number, item: any) => total + item.count, 0)
    );
    const allPrice = computed(() =>
      cartList.value.reduce(
        (total: number, item: any) => total + item.count * item.price,
        0
      )
    );
    // 是否全选
    const isAll = computed(() =>
      cartList.value.every((item: { selected: any }) => item.selected)
    );
    //单项功能
    const singleCheck = (skuId: any, selected: any) => {
      // 通过skuId找到要修改的那一项 然后把它的selected修改为传过来的selected
      const item = cartList.value.find(
        (item: { skuId: any }) => item.skuId === skuId
      );
      item.selected = selected;
    };
    //全选框
    const allCheck = (selected: any) => {
      cartList.value.forEach((item: { selected: any }) => {
        return (item.selected = selected);
      });
    };
    //已选数量
    const selectedCount = computed(() => {
      return cartList.value
        .filter((item: { selected: any }) => item.selected)
        .reduce((total: number, item: any) => total + item.count, 0);
    });
    //已选价格
    const selectedPrice = computed(() => {
      return cartList.value
        .filter((item: { selected: any }) => item.selected)
        .reduce(
          (total: number, item: any) => total + item.count * item.price,
          0
        );
    });
    //清除购物车
    const clearCart = () => {
      cartList.value = [];
    };
    return {
      cartList,
      addCart,
      deleteCart,
      allCount,
      allPrice,
      singleCheck,
      allCheck,
      isAll,
      selectedCount,
      selectedPrice,
      clearCart,
      updateNewList,
    };
  },
  {
    persist: true, // 开启数据持久化
  }
);
